package vn.wallet.app.service.impl;

// Generated Oct 23, 2012 10:05:44 PM by Hibernate Tools 3.4.0.CR1

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.LazyInitializationException;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.wallet.app.dao.IBudgetDAO;
import vn.wallet.app.domain.Budget;
import vn.wallet.app.domain.Payment;
import vn.wallet.app.domain.User;
import vn.wallet.app.service.IBudgetService;

@Transactional
@Service
public class BudgetService implements IBudgetService {

	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(BudgetService.class);

	@Autowired
	IBudgetDAO budgetDAO;

	private User currentUser;

	@Override
	public Budget getById(int id) throws Exception {
		try {
			return budgetDAO.getById(id);
		} catch (Exception ex) {
			throw ex;
		}
	}

	@Override
	public void delete(Budget obj) throws Exception {
		budgetDAO.delete(obj);

	}

	@Override
	public void delete(int id) throws Exception {
		budgetDAO.delete(id);
	}

	@Override
	public void delete(Budget... objs) throws Exception {
		for (Budget budget : objs) {
			budgetDAO.delete(budget);
		}
	}

	@Override
	public void update(Budget obj) throws Exception {
		budgetDAO.update(obj);
	}

	@Override
	public void update(Budget... objs) throws Exception {
		for (Budget budget : objs) {
			budgetDAO.update(budget);
		}
	}

	@Override
	public void save(Budget obj) throws Exception {
		budgetDAO.save(obj);
	}

	@Override
	public void save(Budget... objs) throws Exception {
		for (Budget budget : objs) {
			budgetDAO.save(budget);
		}
	}

	@Override
	public List<Budget> getAll() throws Exception {
		return budgetDAO.getAll();
	}

	@Override
	public List<Budget> getAllByUserId(int userId) throws Exception {
		return budgetDAO.getAllByUserId(userId);
	}

	@Override
	public List<Object> searchUser(int first, int pageSize, String sortField,
			SortOrder sortOrder, Budget budget, boolean ignoreStatus)
			throws Exception {
		return budgetDAO.searchUser(first, pageSize, sortField, sortOrder,
				budget, ignoreStatus);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Object> getTransaction(Budget budget, int UserID) throws Exception {

		ArrayList result = new ArrayList();
		Set<Payment> trans = budget.getPayments();
		try {
			// this will throw exception if has not loaded yet
			result.addAll(trans);
		} catch (LazyInitializationException lazy) {
			// get lazy payments from db
			List<Object> lstPayment = budgetDAO.getTransaction(budget);
			result.addAll(lstPayment);
		} finally {
			// set back payment to budget for un-necessary load
			budget.setPayments(new HashSet<Payment>(result));
		}
		return result;
	}

	@Override
	public List<?> findByExample(Budget obj) throws Exception {
		return budgetDAO.findByExample(obj);
	}

	public IBudgetDAO getBudgetDAO() {
		return budgetDAO;
	}

	public void setBudgetService(IBudgetDAO budgetDAO) {
		this.budgetDAO = budgetDAO;
	}
}
